<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the use of the GNU compilers.

Copyright (C) 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>BPF Built-in Functions (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="BPF Built-in Functions (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="BPF Built-in Functions (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Indices.html#Indices" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Target-Builtins.html#Target-Builtins" rel="up" title="Target Builtins">
<link href="FR_002dV-Built_002din-Functions.html#FR_002dV-Built_002din-Functions" rel="next" title="FR-V Built-in Functions">
<link href="Blackfin-Built_002din-Functions.html#Blackfin-Built_002din-Functions" rel="prev" title="Blackfin Built-in Functions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en_US">
<a name="BPF-Built_002din-Functions"></a>
<div class="header">
<p>
Next: <a href="FR_002dV-Built_002din-Functions.html#FR_002dV-Built_002din-Functions" accesskey="n" rel="next">FR-V Built-in Functions</a>, Previous: <a href="Blackfin-Built_002din-Functions.html#Blackfin-Built_002din-Functions" accesskey="p" rel="prev">Blackfin Built-in Functions</a>, Up: <a href="Target-Builtins.html#Target-Builtins" accesskey="u" rel="up">Target Builtins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="BPF-Built_002din-Functions-1"></a>
<h4 class="subsection">6.60.12 BPF Built-in Functions</h4>

<p>The following built-in functions are available for eBPF targets.
</p>
<dl>
<dt><a name="index-_005f_005fbuiltin_005fbpf_005fload_005fbyte"></a>Built-in Function: <em>unsigned long long</em> <strong>__builtin_bpf_load_byte</strong> <em>(unsigned long long <var>offset</var>)</em></dt>
<dd><p>Load a byte from the <code>struct sk_buff</code> packet data pointed by the register <code>%r6</code> and return it.
</p></dd></dl>

<dl>
<dt><a name="index-_005f_005fbuiltin_005fbpf_005fload_005fhalf"></a>Built-in Function: <em>unsigned long long</em> <strong>__builtin_bpf_load_half</strong> <em>(unsigned long long <var>offset</var>)</em></dt>
<dd><p>Load 16 bits from the <code>struct sk_buff</code> packet data pointed by the register <code>%r6</code> and return it.
</p></dd></dl>

<dl>
<dt><a name="index-_005f_005fbuiltin_005fbpf_005fload_005fword"></a>Built-in Function: <em>unsigned long long</em> <strong>__builtin_bpf_load_word</strong> <em>(unsigned long long <var>offset</var>)</em></dt>
<dd><p>Load 32 bits from the <code>struct sk_buff</code> packet data pointed by the register <code>%r6</code> and return it.
</p></dd></dl>

<dl>
<dt><a name="index-_005f_005fbuiltin_005fpreserve_005faccess_005findex"></a>Built-in Function: <em>void *</em> <strong>__builtin_preserve_access_index</strong> <em>(<var>expr</var>)</em></dt>
<dd><p>BPF Compile Once-Run Everywhere (CO-RE) support. Instruct GCC to generate CO-RE relocation records for any accesses to aggregate data structures (struct, union, array types) in <var>expr</var>. This builtin is otherwise transparent, the return value is whatever <var>expr</var> evaluates to. It is also overloaded: <var>expr</var> may be of any type (not necessarily a pointer), the return type is the same. Has no effect if <code>-mco-re</code> is not in effect (either specified or implied).
</p></dd></dl>

<dl>
<dt><a name="index-_005f_005fbuiltin_005fpreserve_005ffield_005finfo"></a>Built-in Function: <em>unsigned int</em> <strong>__builtin_preserve_field_info</strong> <em>(<var>expr</var>, unsigned int <var>kind</var>)</em></dt>
<dd><p>BPF Compile Once-Run Everywhere (CO-RE) support. This builtin is used to
extract information to aid in struct/union relocations.  <var>expr</var> is
an access to a field of a struct or union. Depending on <var>kind</var>, different
information is returned to the program. A CO-RE relocation for the access in
<var>expr</var> with kind <var>kind</var> is recorded if <code>-mco-re</code> is in effect.
</p>
<p>The following values are supported for <var>kind</var>:
</p><dl compact="compact">
<dt><code>FIELD_BYTE_OFFSET = 0</code></dt>
<dd><p>The returned value is the offset, in bytes, of the field from the
beginning of the containing structure. For bit-fields, this is the byte offset
of the containing word.
</p>
</dd>
<dt><code>FIELD_BYTE_SIZE = 1</code></dt>
<dd><p>The returned value is the size, in bytes, of the field. For bit-fields,
this is the size in bytes of the containing word.
</p>
</dd>
<dt><code>FIELD_EXISTENCE = 2</code></dt>
<dd><p>The returned value is 1 if the field exists, 0 otherwise. Always 1 at
compile time.
</p>
</dd>
<dt><code>FIELD_SIGNEDNESS = 3</code></dt>
<dd><p>The returned value is 1 if the field is signed, 0 otherwise.
</p>
</dd>
<dt><code>FIELD_LSHIFT_U64 = 4</code></dt>
<dt><code>FIELD_RSHIFT_U64 = 5</code></dt>
<dd><p>The returned value is the number of bits of left- or right-shifting
(respectively) needed in order to recover the original value of the field,
after it has been loaded by a read of <code>FIELD_BYTE_SIZE</code> bytes into an
unsigned 64-bit value. Primarily useful for reading bit-field values
from structures that may change between kernel versions.
</p>
</dd>
</dl>

<p>Note that the return value is a constant which is known at
compile time. If the field has a variable offset then
<code>FIELD_BYTE_OFFSET</code>, <code>FIELD_LSHIFT_U64</code>,
and <code>FIELD_RSHIFT_U64</code> are not supported.
Similarly, if the field has a variable size then
<code>FIELD_BYTE_SIZE</code>, <code>FIELD_LSHIFT_U64</code>,
and <code>FIELD_RSHIFT_U64</code> are not supported.
</p>
<p>For example, <code>__builtin_preserve_field_info</code> can be used to reliably
extract bit-field values from a structure that may change between
kernel versions:
</p>
<div class="smallexample">
<pre class="smallexample">struct S
{
  short a;
  int x:7;
  int y:5;
};

int
read_y (struct S *arg)
{
  unsigned long long val;
  unsigned int offset
    = __builtin_preserve_field_info (arg-&gt;y, FIELD_BYTE_OFFSET);
  unsigned int size
    = __builtin_preserve_field_info (arg-&gt;y, FIELD_BYTE_SIZE);

  /* Read size bytes from arg + offset into val.  */
  bpf_probe_read (&amp;val, size, arg + offset);

  val &lt;&lt;= __builtin_preserve_field_info (arg-&gt;y, FIELD_LSHIFT_U64);

  if (__builtin_preserve_field_info (arg-&gt;y, FIELD_SIGNEDNESS))
    val = ((long long) val
           &gt;&gt; __builtin_preserve_field_info (arg-&gt;y, FIELD_RSHIFT_U64));
  else
    val &gt;&gt;= __builtin_preserve_field_info (arg-&gt;y, FIELD_RSHIFT_U64);

  return val;
}

</pre></div>
</dd></dl>

<hr>
<div class="header">
<p>
Next: <a href="FR_002dV-Built_002din-Functions.html#FR_002dV-Built_002din-Functions" accesskey="n" rel="next">FR-V Built-in Functions</a>, Previous: <a href="Blackfin-Built_002din-Functions.html#Blackfin-Built_002din-Functions" accesskey="p" rel="prev">Blackfin Built-in Functions</a>, Up: <a href="Target-Builtins.html#Target-Builtins" accesskey="u" rel="up">Target Builtins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html#Indices" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
